import pandas as pd

from models.Country import Country


class CountryData(object):
    """
    国家列表，可以通过国家简称查询对应国家信息
    使用 单例模式
    """

    def __new__(cls, *args, **kwargs):
        if not hasattr(CountryData, "_instance"):
            CountryData._init_flag = True
            CountryData._instance = super().__new__(cls)
        return CountryData._instance

    def __init__(self, file_path):
        if not hasattr(CountryData, "_init"):
            CountryData._init = True
            print(">>> 初始化 <<<")
            self.file_path = file_path
            self.countries = {}
            self.countries_chn = {}
            self.load_countries_from_excel()

    def load_countries_from_excel(self):
        df = pd.read_excel(self.file_path, sheet_name='Africa')
        for index, row in df.iterrows():
            country = Country(row['英文名称'], row['中文名称'], row['缩写'], row['首都英文'], row['首都中文'])
            # 存储对象
            self.countries[row['缩写']] = country
            # 存储一个KV形式的中文名，便于查找
            self.countries_chn[row['缩写']] = row['中文名称']

    def get_sorted_keys(self):
        sorted_keys = sorted(self.countries.keys())
        return sorted_keys

    def get_country(self, abbr):
        print(abbr)
        return self.countries.get(abbr)

    def get_country_abbrs(self):
        return self.countries.keys()


countryAfrica = CountryData(r'D:\01.DOC\漏损产品\市场分析\非洲国家数据.xlsx')

if __name__ == '__main__':
    s1 = CountryData(r'D:\01.DOC\漏损产品\市场分析\非洲国家数据.xlsx')
    s2 = CountryData('s2')
    print(s1, s2)
    print(s1 is s2)
    count = s1.get_country("DZA")
    print(count)
    print(s2.get_country("IRQ"))
    # count.display_info()
    # s1.print_name()
    # s2.print_name()
